package com.yuntian.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuntian.Dto.UserDto;
import com.yuntian.Dto.UserPageQueryDto;
import com.yuntian.Vo.admin.UserVo;
import com.yuntian.annotation.Log;
import com.yuntian.entity.User;
import com.yuntian.mapper.UserMapper;
import com.yuntian.result.PageResult;
import com.yuntian.service.UserService;
import com.yuntian.util.IdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //登录
    @Log(value = "登录", type = "SELECT")
    public User login(User user){
        String username = user.getUsername();
        User user1 =  userMapper.selectByUsername(username);
        if (user1 == null){
            System.out.println("用户名不存在");
        }
        if (user1.getUserStatus() == 0){
            System.out.println("账号已被禁用");
        }
        String userpass = user.getUserpass();
        //密码加密，
        userpass = DigestUtils.md5DigestAsHex(userpass.getBytes());
        if (!user1.getUserpass().equals(userpass)){
            System.out.println("密码错误");
        }
        return user1;
    }

    //查找
    public PageResult selectUserList(UserPageQueryDto userPageQueryDto){
        PageHelper.startPage(userPageQueryDto.getPage(),userPageQueryDto.getPageSize());
        Page<User> page = userMapper.selectUserList(userPageQueryDto);
        long total = page.getTotal();
        List<User> result = page.getResult();
        List<UserDto> userDtoList = new ArrayList<>();
        for (User user : result) {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(user,userDto);
            userDtoList.add(userDto);
        }

        return new PageResult(total,userDtoList);
    }

    //注册
    //md5加密
    public void register(User user){
        String username = user.getUsername();
        if (userMapper.selectByUsername(username) != null){
            throw new RuntimeException("用户名已存在");
        }
        IdUtil id = new IdUtil();
        user.setUserId(id.getRandomId());
        user.setUserStatus(1);
        user.setRegisterTime(LocalDateTime.now());

        String userPass = user.getUserpass();
        user.setUserpass(DigestUtils.md5DigestAsHex(userPass.getBytes()));
        userMapper.insertUser(user);
    }

    //删除
    public void deleteUser(long userId){
        userMapper.deleteUser(userId);
    }

    //修改
    public void updateUser(User user){

        user.setRegisterTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    //根据id查询用户
    @Override
    public UserVo getByUserId(Long userId) {
        User user =  userMapper.getByUserId(userId);
        //属性拷贝
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);

        return userVo;
    }

    @Override
    public void startOrStop(User user) {
        user.setRegisterTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    /**
     * 统计用户数量
     * @return
     */
    @Override
    public Integer count() {
        return userMapper.count();
    }


}
